﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Moonwish.Silverlight.Controls
{
    public class StartPrograme : Control
    {
        private Button _btnStart;
        private Popup _popup;
        private Canvas _outsideCanvas;
        private Canvas _popupContentCanvas;

        public StartPrograme()
        {
            this.DefaultStyleKey = typeof(StartPrograme);
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            _popup = (Popup) GetTemplateChild("Popup");

            _btnStart = (Button) GetTemplateChild("btnStart");

            //_popupContentCanvas = new Canvas();
            //_popupContentCanvas.Background = new SolidColorBrush(Colors.LightGray);

            //_outsideCanvas = new Canvas();
            //_outsideCanvas.Background = new SolidColorBrush(Colors.Transparent);

            //var popupContent = _popup.Child;

            //_popup.Child = _popupContentCanvas;

            //_popupContentCanvas.Children.Add(_outsideCanvas);
            //_popupContentCanvas.Children.Add(popupContent);

            
            _btnStart.Click += BtnStart_Click;
        }

        void BtnStart_Click(object sender, RoutedEventArgs e)
        {
            _popup.IsOpen = !_popup.IsOpen;
        }

        protected override Size MeasureOverride(Size availableSize)
        {
            if (_popup != null)
            {
                var popupContent = (Border) _popup.Child;
                var content = Application.Current.Host.Content;
                popupContent.Width = content.ActualWidth;
                popupContent.Height = content.ActualHeight;
                Canvas.SetZIndex(_btnStart, 1000);
                _popup.IsOpen = true;
            }

            return base.MeasureOverride(availableSize);
        }

        private void ArrangePopup()
        {
            var transform = TransformToVisual(null);
            if (transform != null)
            {
                var content = Application.Current.Host.Content;
                _popupContentCanvas.Width = content.ActualWidth;
                _popupContentCanvas.Height = content.ActualHeight;

                var point = new Point(0.0, 0.0);

                var point2 = new Point(1.0, 0.0);

                var point3 = new Point(0.0, 1.0);

                var point4 = transform.Transform(point);

                var point5 = transform.Transform(point2);

                var point6 = transform.Transform(point3);

                var identity = Matrix.Identity;

                identity.M11 = point5.X - point4.X;

                identity.M12 = point5.Y - point4.Y;

                identity.M21 = point6.X - point4.X;

                identity.M22 = point6.Y - point4.Y;

                identity.OffsetX -= point4.X;

                identity.OffsetY -= point4.Y;

                var transform2 = new MatrixTransform

                {

                    Matrix = identity

                };
                _popupContentCanvas.RenderTransform = transform2;
            }
        }
    }
}
